﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using ProgNetComponents.Data.Query;
using System.Windows.Forms;

namespace ProgNetComponents.Design.Designers
{
    public abstract class BaseCollectionContainerDesigner : ComponentDesigner
    {
        protected abstract string ItemsPropName
        {
            get;
        }
        
        public override DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection list = new DesignerVerbCollection();
                list.Add(new DesignerVerb("Edit Items", new EventHandler(EditItems)));
                return list;
            }
        }

        protected virtual void EditItems(object sender, EventArgs e)
        {
            PropertyDescriptor prop = TypeDescriptor.GetProperties(Component)["Items"];

            UITypeEditor editor = prop.GetEditor(typeof(UITypeEditor)) as UITypeEditor;
            object obj = prop.GetValue(Component);
            editor.EditValue(new BaseContainerContext(this, prop)
                , new BaseContainerContext(this, prop), obj);
        }
    }
    public class SelectDataContainerDesigner : BaseCollectionContainerDesigner
    {
        public override DesignerVerbCollection Verbs
        {
            get
            {
                DesignerVerbCollection list = base.Verbs;
                list.Add(new DesignerVerb("Create Constant Class", new EventHandler(GenerateClass)));
                return list;
            }
        }
        protected ProgNetComponents.Data.Query.SelectDataContainer Container
        {
            get { return Component as ProgNetComponents.Data.Query.SelectDataContainer; }
        }

        private void GenerateClass(object sender, EventArgs e)
        {
            if (Container != null)
            {
                List<string> list = GetItems();
                string ClassName = "SelectDataNames";
                string Namespace = ProgNetComponents.Constants.ProgNetConstNamespace;
                ProgNetComponents.Design.Serialization.ProjectHelper.AddConstantStringClass(Component, Namespace, ClassName, list);
            }
        }

        protected List<string> GetItems()
        {
            List<string> l = new List<string>();
            foreach (SelectData d in Container.Items)
            {
                if (!l.Contains(d.Name))
                {
                    l.Add(d.Name);
                }
            }
            return l;
        }

        protected override string ItemsPropName
        {
            get { return "Items"; }
        }
    }

    public class BaseContainerContext : IWindowsFormsEditorService, ITypeDescriptorContext, IServiceProvider
    {
        private IDesigner designer;
        private PropertyDescriptor prop;
        private IComponentChangeService changeService;
        public BaseContainerContext(IDesigner designer, PropertyDescriptor prop)
        {
            this.designer = designer;
            this.prop = prop;
        }
        private IComponentChangeService ChangeService
        {
            get
            {
                if (this.changeService == null)
                {
                    this.changeService = (IComponentChangeService)((IServiceProvider)this).GetService(typeof(IComponentChangeService));
                }
                return this.changeService;
            }
        }

        #region ITypeDescriptorContext Members

        public IContainer Container
        {
            get
            {
                if (this.designer.Component.Site != null)
                {
                    return this.designer.Component.Site.Container;
                }
                return null;
            }
        }

        public object Instance
        {
            get
            {
                return this.designer.Component;
            }
        }

        public void OnComponentChanged()
        {
            this.ChangeService.OnComponentChanged(designer.Component, prop, null, null);
        }

        public bool OnComponentChanging()
        {
            try
            {
                this.ChangeService.OnComponentChanging(designer.Component, prop);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public PropertyDescriptor PropertyDescriptor
        {
            get { return prop; }
        }

        #endregion

        #region IWindowsFormsEditorService Members

        public void CloseDropDown()
        {

        }

        public void DropDownControl(System.Windows.Forms.Control control)
        {

        }

        public System.Windows.Forms.DialogResult ShowDialog(System.Windows.Forms.Form dialog)
        {
            IUIService service = (IUIService)((IServiceProvider)this).GetService(typeof(IUIService));
            if (service != null)
            {
                return service.ShowDialog(dialog);
            }
            return dialog.ShowDialog(this.designer.Component as IWin32Window);

        }

        #endregion

        #region IServiceProvider Members

        object IServiceProvider.GetService(Type serviceType)
        {
            if ((serviceType == typeof(ITypeDescriptorContext)) || (serviceType == typeof(IWindowsFormsEditorService)))
            {
                return this;
            }
            if (this.designer.Component.Site != null)
            {
                return this.designer.Component.Site.GetService(serviceType);
            }
            return null;

        }

        #endregion
    }
}
